// SetROI.cpp : implementation file
//

#include "stdafx.h"
#include "Virtual Pole.h"
#include "SetROI.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetROI dialog

CSetROI::CSetROI(CWnd* pParent /*=NULL*/)
	: CDialog(CSetROI::IDD, pParent)
	, m_csROIBandwidth(_T(""))
	, m_iFPSCalc(1000)
{
	//{{AFX_DATA_INIT(CSetROI)
	//}}AFX_DATA_INIT
	m_FullFrame = CSize(VP_ROI_MAX_WIDTH, VP_ROI_MAX_HEIGHT);
}

void CSetROI::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetROI)
	DDX_Text(pDX, IDC_ROI_EDIT_BOTTOM, m_lROI_Bottom);
	DDV_MinMaxLong(pDX, m_lROI_Bottom, 0, VP_ROI_MAX_HEIGHT);
	DDX_Text(pDX, IDC_ROI_EDIT_HEIGHT, m_lROI_Height);
	DDV_MinMaxLong(pDX, m_lROI_Height, 0, VP_ROI_MAX_HEIGHT);
	DDX_Text(pDX, IDC_ROI_EDIT_LEFT, m_lROI_Left);
	DDV_MinMaxLong(pDX, m_lROI_Left, 0, VP_ROI_MAX_WIDTH);
	DDX_Text(pDX, IDC_ROI_EDIT_WIDTH, m_lROI_Width);
	DDV_MinMaxLong(pDX, m_lROI_Width, 0, VP_ROI_MAX_WIDTH);
	//}}AFX_DATA_MAP
	DDX_Text(pDX, IDC_ROI_BANDWIDTH, m_csROIBandwidth);
	DDX_Text(pDX, IDC_ROI_EDIT_WIDTH2, m_iFPSCalc);
	DDV_MinMaxInt(pDX, m_iFPSCalc, 0, 10000);
}


BEGIN_MESSAGE_MAP(CSetROI, CDialog)
	//{{AFX_MSG_MAP(CSetROI)
	ON_BN_CLICKED(ID_ROI_BTN, OnROIBtn)
	ON_BN_CLICKED(ID_ROI_BTN_FULL, OnROIBtnFull)
	//}}AFX_MSG_MAP
	ON_STN_CLICKED(IDC_ROI_BANDWIDTH, &CSetROI::OnStnClickedRoiBandwidth)
	ON_BN_CLICKED(ID_ROI_BTN_FULL2, &CSetROI::OnBnClickedRoiBtnFull2)
	ON_EN_CHANGE(IDC_ROI_EDIT_WIDTH, &CSetROI::OnEnChangeRoiEditWidth)
	ON_EN_CHANGE(IDC_ROI_EDIT_HEIGHT, &CSetROI::OnEnChangeRoiEditHeight)
	ON_EN_CHANGE(IDC_ROI_EDIT_WIDTH2, &CSetROI::OnEnChangeRoiEditWidth2)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetROI message handlers

void CSetROI::OnROIBtn() {
	m_bROI_Mode = true;
	OnCancel();
	OnBnClickedRoiBtnFull2();
}

int CSetROI::DoModal() {
	m_bROI_Mode = false;
	return CDialog::DoModal();
	OnBnClickedRoiBtnFull2();
}

void CSetROI::OnROIBtnFull() {
	UpdateData(true);
	m_lROI_Bottom	= 0;
	m_lROI_Left		= 0;
	m_lROI_Width	= m_FullFrame.cx;
	m_lROI_Height	= m_FullFrame.cy;
	UpdateData(false);
	OnBnClickedRoiBtnFull2();
}

void CSetROI::OnCancel() {
	CDialog::OnCancel();
}

void CSetROI::OnOK() {
	UpdateData(true);
	if (m_lROI_Width + m_lROI_Left > m_FullFrame.cx) {
		AfxMessageBox("ROI horizontal extent exceeds image boundaries", MB_ICONINFORMATION | MB_OK);
		return;
	}
	if (m_lROI_Height + m_lROI_Bottom > m_FullFrame.cy) {
		AfxMessageBox("ROI vertical extent exceeds image boundaries", MB_ICONINFORMATION | MB_OK);
		return;
	}
	CDialog::OnOK();
}

void CSetROI::OnStnClickedRoiBandwidth() {
}

void CSetROI::OnBnClickedRoiBtnFull2() {
	UpdateData(true);
	// Bandwidth: (xx * yy) pixels * FPS = MPixels/sec
	//double nPixRes = double(m_lROI_Width * m_lROI_Height);
	//double fBandwidth = (nPixRes * (double)m_iFPSCalc) / 1000000;

	// Bandwidth: Width x Height x Frames/sec = bytes/sec
	double nPixRes = double(m_lROI_Width * m_lROI_Height); // width * height
	double fBandwidth = nPixRes * (double)m_iFPSCalc; // bytes/sec
	fBandwidth = fBandwidth / 1048576; // Mbytes/sec

	m_csROIBandwidth.Format("%.0f MB/sec", fBandwidth);
	UpdateData(false);
}

void CSetROI::OnEnChangeRoiEditWidth() {
	OnBnClickedRoiBtnFull2();
}

void CSetROI::OnEnChangeRoiEditHeight() {
	OnBnClickedRoiBtnFull2();
}

void CSetROI::OnEnChangeRoiEditWidth2() {
	OnBnClickedRoiBtnFull2();
}
